package com.njworkorder.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njworkorder.DTO.MeetingRegistrationAddDto;
import com.njworkorder.Entity.MeetingRegistration;
import com.njworkorder.Service.MeetingRegistrationService;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;


@Tag(name = "会议登记")
@RestController
@RequestMapping("/meeting")
public class MeetingRegistrationController {

    private final MeetingRegistrationService meetingRegistrationService;
    public MeetingRegistrationController(MeetingRegistrationService meetingRegistrationService) {
        this.meetingRegistrationService = meetingRegistrationService;
    }

    @Operation(summary = "添加会议登记信息")
    @PostMapping("/addInfo")
    public Result<String> addInfo(@RequestBody MeetingRegistrationAddDto meetingRegistrationAddDto) {
        MeetingRegistration meetingRegistration = new MeetingRegistration();
        BeanUtils.copyProperties(meetingRegistrationAddDto, meetingRegistration);
        return meetingRegistrationService.save(meetingRegistration) ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败！");
    }

    @Operation(summary = "根据id删除会议登记信息")
    @GetMapping("/deleteById")
    public Result<String> deleteById(@RequestParam("id") String id) {
        return meetingRegistrationService.removeById(id) ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败！");
    }


    @Operation(summary = "根据id更新会议登记信息")
    @PostMapping("/updateById/{id}")
    public Result<String> updateById(@RequestBody MeetingRegistrationAddDto meetingRegistrationAddDto, @PathVariable("id") String id) {
        MeetingRegistration meetingRegistration = new MeetingRegistration();
        BeanUtils.copyProperties(meetingRegistrationAddDto, meetingRegistration);
        meetingRegistration.setId(id);
        return meetingRegistrationService.updateById(meetingRegistration) ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败！");
    }

    @Operation(summary = "根据id查询会议登记信息")
    @GetMapping("/getById")
    public Result<MeetingRegistration> getById(@RequestParam("id") String id) {
        return ResultUtil.success(meetingRegistrationService.getById(id));
    }

    @Operation(summary = "获取会议登记信息列表")
    @GetMapping("/getList")
    public Result<Page<MeetingRegistration>> getList(@RequestParam("pageIndex") int pageIndex,
                                                     @RequestParam("pageSize") int pageSize,
                                                     @RequestParam(value = "meetingName",required = false) String meetingName) {
        LambdaQueryWrapper<MeetingRegistration> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(meetingName),MeetingRegistration::getMeetingName,meetingName);
        Page<MeetingRegistration> page = meetingRegistrationService.page(new Page<>(pageIndex, pageSize), queryWrapper);
        return ResultUtil.success(page);
    }




}
